<template>
  <div class="dish-card">
    <!-- 菜品图片 -->
    <div class="dish-image">
      <img :src="dish.image" :alt="dish.name" class="food-img">
    </div>

    <!-- 菜品信息 -->
    <div class="dish-info">
      <h3 class="dish-name">{{ dish.name }}</h3>
      <p class="dish-desc">{{ dish.description || '暂无描述' }}</p>
      <div class="dish-meta">
        <span class="dish-price">¥{{ dish.price.toFixed(2) }}</span>
        <span class="dish-category" v-if="dish.categoryName">
          {{ dish.categoryName }}
        </span>
        <span class="dish-status" :class="dish.status === 1 ? 'status-active' : 'status-inactive'">
          {{ dish.status === 1 ? '在售' : '停售' }}
        </span>
      </div>
    </div>

    <!-- 数量控制 -->
    <div class="dish-control" v-if="dish.status === 1">
      <button class="count-btn" @click="decrement" :disabled="count <= 0">-</button>
      <span class="count">{{ count }}</span>
      <button class="count-btn" @click="increment">+</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

// 接收父组件传入的菜品数据
const props = defineProps({
  dish: {
    type: Object,
    required: true,
    default: () => ({
      id: 0,
      name: '',
      price: 0,
      image: '',
      description: '',
      status: 0,
      categoryName: ''
    })
  }
});

// 菜品数量
const count = ref(0);

// 减少数量
const decrement = () => {
  if (count.value > 0) {
    count.value--;
    // 可以在这里触发自定义事件，通知父组件数量变化
    emit('count-change', {
      dishId: props.dish.id,
      count: count.value
    });
  }
};

// 增加数量
const increment = () => {
  count.value++;
  // 触发自定义事件
  emit('count-change', {
    dishId: props.dish.id,
    count: count.value
  });
};

// 定义自定义事件
const emit = defineEmits(['count-change']);
</script>

<style scoped>
.dish-card {
  display: flex;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  gap: 16px;
}

.dish-image {
  width: 100px;
  height: 100px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.food-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.food-img:hover {
  transform: scale(1.05);
}

.dish-info {
  flex-grow: 1;
  min-width: 0;
}

.dish-name {
  font-size: 18px;
  margin: 0 0 8px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dish-desc {
  color: #666;
  font-size: 14px;
  margin: 0 0 12px 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.dish-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.dish-price {
  color: #ff4d4f;
  font-weight: bold;
  font-size: 16px;
}

.dish-category {
  font-size: 12px;
  color: #888;
  background: #f5f5f5;
  padding: 2px 8px;
  border-radius: 4px;
}

.dish-status {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 4px;
}

.status-active {
  color: #52c41a;
  background: #f6ffed;
  border: 1px solid #b7eb8f;
}

.status-inactive {
  color: #888;
  background: #f5f5f5;
  border: 1px solid #ddd;
}

.dish-control {
  display: flex;
  align-items: center;
  gap: 8px;
}

.count-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: all 0.2s;
}

.count-btn:hover {
  background: #f5f5f5;
}

.count-btn:disabled {
  color: #ccc;
  cursor: not-allowed;
}

.count {
  width: 28px;
  text-align: center;
}
</style>
